package com.wwh.furns.web;

import com.wwh.furns.entity.Member;
import com.wwh.furns.service.MemberService;
import com.wwh.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Albert
 * @date 2024/5/10-22:55
 * @describe
 */
public class LoginServlet extends HttpServlet {
    private MemberService memberService = new MemberServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("RegisterServlet is called...");

        //接收用户登录信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        req.setAttribute("username", username);

        //判断用户名是否可登录
        Member member = memberService.loginMember(new Member(null, username, password, null));
        if (member != null) {
            //登录
            //System.out.println("登录成功");
            req.getRequestDispatcher("/views/member/login_ok.jsp").forward(req, resp);
        } else {
            //System.out.println("登录失败");
            req.setAttribute("errorMsg", "用户名或密码错误~");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);

        }
    }
}
